<?php

namespace App\Dep;

use App\Models\MainModel;

class MainDep
{
    public $model;

    public function __construct()
    {
        $this->model = new MainModel();
    }

    public function first($id)
    {
        $res = $this->model->where('id', $id)->first();
        return $res;
    }

    public function all()
    {

        $res = $this->model->all();

        return $res;
    }

    public function add($data)
    {
        $res = $this->model->insertGetId($data);
        return $res;
    }

    public function edit($id, $data)
    {
        $res = $this->model->where('id', $id)->update($data);
        return $res;
    }

    public function batchEdit($ids, $data)
    {
        $res = $this->model->whereIn('id', $ids)->update($data);
        return $res;
    }

    public function del($id)
    {
        $res = $this->model->where('id', $id)->delete();
        return $res;
    }

    // 新增方法
    public function setIsUsed($id)
    {
        // 首先将所有记录的 isUsed 字段设置为 0
        $this->model->where('isUsed', 1)->update(['isUsed' => 0]);

        // 然后将指定 ID 的记录的 isUsed 字段设置为 1
        $res = $this->model->where('id', $id)->update(['isUsed' => 1]);

        return $res;
    }

}
